﻿var UITypeData=[{name:"hidden",code:"0"},{name:"Text",code:"1"},{name:"DateTime",code:"2"},{name:"TextArea",code:"3"},{name:"Date",code:"4"},{name:"Int",code:"5"},{name:"Float",code:"6"},{name:"Select",code:"7"}];

//初始化入口
$(function() {
    $('#dlg-addClass').dialog('close');
    $('#dlg-addTable').dialog('close');
    $('#dlg-editField').dialog('close');
    $('#dlg-showunitdata').dialog('close');
    fn_Tree();

    $('#fieldUIType').combobox('loadData', UITypeData);
    $('#fieldUIType').combobox('options').onSelect = function(record) {
        if (record.name == "Select") {
            $('#fieldDataUnitId').combobox('enable');
            $('#fieldDynamicSQL').removeAttr('readonly');

        } else {
            $('#fieldDataUnitId').combobox('disable');
            $('#fieldDynamicSQL').attr('readonly', 'true');
        }
    };
    $('#fieldDataUnitId').combobox('options').onSelect = function(record) {
        $('#fieldDynamicSQL').val(record.UnitSQL);
    };
    fn_InitCheckBox();


});

function fn_Tree() {
    $('#treeload').tree({
        url: "Controller.aspx?controller=GeneralController&method=LoadLayers",
        onClick: function(node) {
            $(this).tree('toggle', node.target);
            if (parseInt(node.attributes.type) == 1) {

                $('#fieldDataUnitId').combobox({ url: "Controller.aspx?controller=GeneralController&method=GetDataUnit" });
                $('#fieldgrid').datagrid('options').url = "Controller.aspx?controller=GeneralController&method=QueryFieldList&titleId=" + node.id;
                $('#fieldgrid').datagrid('reload');

                requestAjax('Controller.aspx?controller=GeneralController&method=LoadPageResultData&titleId=' + node.id, {}, function(data) {
                    $('#resultDiv').empty();
                    $('#resultDiv').load("GeneralData_resultData.aspx", {}, function() {
                        $.parser.parse($('#resultDiv'));
                    });
                });
            } else {
                $('#resultDiv').empty();
            }
        }
    });
}

//添加分类
function btn_AddClass(){
    var selected=$('#treeload').tree('getSelected');
    if(selected){
        if(parseInt(selected.attributes.type)==0){
            $("#formaddclass").form('clear');
            $("#formGeneralLayerId").val(0);
            $("#formPGeneralLayerId").val(selected.id);
            $('#dlg-addClass').dialog({title:'添加分类'});
        }
    }
}
//确认添加分类
function dlg_addClassOk(){
    FormSubmit($('#formaddclass'), 'Controller.aspx?controller=GeneralController&method=AddGeneralLayer', function() {
                    $('#dlg-addClass').dialog('close');
                    $('#treeload').tree('reload');
                });
}

//添加表
function btn_AddTable(){
    var selected=$('#treeload').tree('getSelected');
    if(selected){
        if(parseInt(selected.attributes.type)==0){
            $("#formaddtable").form('clear');
            $("#formtitileId").val(0);
            $("#formPLayerId").val(selected.id);
            $('#dlg-addTable').dialog({title:'添加表'});
        }
    }
}

function dlg_addTableOk(){
    FormSubmit($('#formaddtable'), 'Controller.aspx?controller=GeneralController&method=AddGeneralTitle', function() {
                    $('#dlg-addTable').dialog('close');
                    $('#treeload').tree('reload');
                });
}

function btn_AlterTree(){
    var selected=$('#treeload').tree('getSelected');
    if(selected){
        if(parseInt(selected.id)==-1)return false;
        if(parseInt(selected.attributes.type)==0){
            $("#formaddclass").form('clear');
            $("#formGeneralLayerId").val(selected.id);
            $("#formPGeneralLayerId").val(selected.attributes.PLayerId);
            $("#formclassname").val(selected.text);
            $('#dlg-addClass').dialog({title:'修改分类'});
        }else{
            $("#formaddtable").form('clear');
            $("#formtitileId").val(selected.id);
            $("#formPLayerId").val(selected.attributes.LayerId);
            $("#formtablename").val(selected.text);
            $("#formtabletable").combobox('setValue',selected.attributes.Table);
            $("#formtablememo").val(selected.attributes.Memo);
            $('#dlg-addTable').dialog({title:'修改表'});
        }
    }
}

function btn_DelTree(){
    var selected=$('#treeload').tree('getSelected');
    if(selected){
        if(parseInt(selected.id)==-1)return false;
        if(parseInt(selected.attributes.type)==0){
            $.messager.confirm('询问', '是否要删除此记录？', function(r){
				if (r){
					 requestAjax('Controller.aspx?controller=GeneralController&method=DeleteGeneralLayer', { GeneralLayerId: selected.id }, function(data) {
                            $('#treeload').tree('reload');
                        });
				}
			});
        }else{
            $.messager.confirm('询问', '是否要删除此记录？', function(r){
				if (r){
					 requestAjax('Controller.aspx?controller=GeneralController&method=DeleteGeneralTitle', { TitleId: selected.id }, function(data) {
                            $('#treeload').tree('reload');
                        });
				}
			});
        }
    }
}
//初始化字段
function btngrid_onInitField(){
    var selected=$('#treeload').tree('getSelected');
    if(selected){
        if(parseInt(selected.attributes.type)==1){
            $.messager.confirm('询问', '初始化会删除现有所有数据，确定要初始化吗？', function(r){
				if (r){
					 requestAjax('Controller.aspx?controller=GeneralController&method=InitField', { TitleId: selected.id,Table:selected.attributes.Table }, function(data) {
                            $('#fieldgrid').datagrid('options').url = "Controller.aspx?controller=GeneralController&method=QueryFieldList&titleId="+selected.id;
                            $('#fieldgrid').datagrid('reload');
                        });
				}
			});
		}
    }
}
//编辑字段
function btngrid_onEditField(){
    var selected=$('#fieldgrid').datagrid('getSelected');
    if(selected){
        $('#formeditfield').form('load',selected);
        if(parseInt(selected.IsKey)==1)
            $("#ckIsKey").attr("checked","true");
        else
            $("#ckIsKey").removeAttr("checked");
            
        if(parseInt(selected.IsMust)==1)
            $("#ckIsMust").attr("checked","true");
        else
            $("#ckIsMust").removeAttr("checked");
        
        if(selected.UIType=="7"){
            $('#fieldDataUnitId').combotree('enable');
            $('#fieldDynamicSQL').removeAttr('readonly');
        }else{    
            $('#fieldDataUnitId').combotree('disable');
            $('#fieldDynamicSQL').attr('readonly','true');
        }
            
        $('#dlg-editField').dialog('open');
    }
}

function dlg_editFieldOk(){
     FormSubmit($('#formeditfield'), 'Controller.aspx?controller=GeneralController&method=UpdateGeneraField', function() {
                    $('#dlg-editField').dialog('close');
                    $('#fieldgrid').datagrid('reload');
                });
}
//上移
function btngrid_onUpField(){
}
//下移
function btngrid_onDownField(){
}
//显示单元数据
function ShowUnitData() {
    var selected = $('#fieldDataUnitId').combobox('getValue');
     if (selected) {
         $('#dlg-showunitdata').dialog('open');
         $('#showunitGrid').datagrid('options').url = "Controller.aspx?controller=GeneralController&method=GetUnitData&dataunitId=" + selected + "&unitSql=" + $('#fieldDynamicSQL').val();
         $('#showunitGrid').datagrid('reload');
     }
}

function fn_InitCheckBox() {
    $("#ckIsKey").click(function() {
        if ($("#ckIsKey").attr("checked")) {
            $("#fieldIsKey").val(1);
        }
        else {
            $("#fieldIsKey").val(0);
        }
    });
    $("#ckIsMust").click(function() {
        if ($("#ckIsMust").attr("checked")) {
            $("#fieldIsMust").val(1);
        }
        else {
            $("#fieldIsMust").val(0);
        }
    });
}

function UITypeFormatter(value){
    for(var i=0; i<UITypeData.length; i++){
		if (UITypeData[i].code == value) return UITypeData[i].name;
	}
	return value;
}